package com.yaoweigroup.community.adapter;

import java.util.List;

import com.yaoweigroup.community.R;
import com.yaoweigroup.community.enity.DiscountOrder;
import com.yaoweigroup.community.enity.MenuInfo;
import com.yaoweigroup.community.enity.OrderInfo;
import com.yaoweigroup.community.interfaze.Callback;
import com.yaoweigroup.community.util.Tools;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MyDsicountOrderAdapter extends BaseAdapter {

	private Context context;
	private List<DiscountOrder> list;
	private LayoutInflater mInflater;
	public Callback callback;

	// ，，，，，，

	public MyDsicountOrderAdapter(Context context, List<DiscountOrder> list) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = mInflater.inflate(
					R.layout.adapter_my_discount_list_item, null);
			convertView.setTag(holder);
			holder.icon_iv = (ImageView) convertView.findViewById(R.id.icon_iv);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.content_tv = (TextView) convertView
					.findViewById(R.id.content_tv);
			holder.money_num_tv = (TextView) convertView
					.findViewById(R.id.money_num_tv);
			holder.reason_tv = (TextView) convertView
					.findViewById(R.id.reason_tv);
			holder.submit_bt = (Button) convertView
					.findViewById(R.id.submit_bt);
			holder.cancel_bt = (Button) convertView
					.findViewById(R.id.cancel_bt);
			holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
			holder.orderNum_tv = (TextView) convertView
					.findViewById(R.id.orderNum_tv);
		} else {
			holder = (Holder) convertView.getTag();
		}

		final DiscountOrder orderInfo = list.get(position);
		holder.submit_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				callback.callback(orderInfo.getOrderNum(), 1,
						Double.parseDouble(orderInfo.getPreferentialPrice()));
			}
		});
		holder.cancel_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				callback.callback(orderInfo.getOrderNum(), 0,
						Double.parseDouble(orderInfo.getPreferentialPrice()));
			}
		});
		holder.time_tv.setText("下单时间：" + orderInfo.getCreateDate());

		holder.content_tv.setText(orderInfo.getPreferentialName());
		String price = "0";
		try {
			price = Tools.round(
					Double.parseDouble(orderInfo.getPreferentialPrice()), 2)
					+ "";
		} catch (Exception e) {
			Log.e("wxf", orderInfo.getPreferentialPrice());
		}

		holder.money_num_tv.setText("￥" + price);
		int state = orderInfo.getState();
		// 0:未付款，1:已付款，2:已使用，3:客户取消，4:管理员取消，5:已退货
		switch (state) {
		case 0:

			holder.reason_tv.setVisibility(View.GONE);
			holder.submit_bt.setVisibility(View.VISIBLE);
			holder.cancel_bt.setVisibility(View.VISIBLE);

			break;
		case 1:
			// 1:已付款
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);
			holder.reason_tv.setText("已购买");
			break;
		case 2:
			// 2:已发货
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);
			holder.reason_tv.setText("已使用");
			break;
		case 3:
			// 3:已取消
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);
			holder.reason_tv.setText("已取消");
			break;
		case 4:
			// 4:已完成
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);
			holder.reason_tv.setText("已取消");
			break;
		case 5:
			// 5:客户取消
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);
			holder.reason_tv.setText("已退货");
			break;

		}
		holder.orderNum_tv.setText("订单号:" + orderInfo.getOrderNum());
		// holder.reason_tv.setText(orderInfo.getPayStateDes());
		return convertView;
	}

	public final class Holder {

		TextView name_tv;
		ImageView icon_iv;
		TextView content_tv;
		TextView money_num_tv;
		Button submit_bt;
		Button cancel_bt;
		TextView reason_tv;
		TextView time_tv;
		TextView orderNum_tv;
	}

	public void setCallback(Callback callback) {
		this.callback = callback;

	}

	public void setDataChange(List<DiscountOrder> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
